import logging
import os.path
from logging.handlers import TimedRotatingFileHandler

from flask import Flask


def init_app(app: Flask):
    log_folder = os.path.join(os.getcwd(), "storage", "log")
    if not os.path.exists(log_folder):
        os.makedirs(log_folder)
    log_file = os.path.join(log_folder, "app.log")
    handler = TimedRotatingFileHandler(
        log_file,
        when='midnight',  # 午夜
        interval=1,  # 处理间隔
        backupCount=30,  # 备份的数量
        encoding='utf-8',
    )
    formatter = logging.Formatter(
        '[%(asctime)s.%(msecs)03d] %(filename)s -> %(funcName)s line:%(lineno)d  [%(levelname)s]:%(message)s'
    )
    handler.setLevel(logging.DEBUG)
    handler.setFormatter(formatter)
    logging.getLogger().addHandler(handler)
    if app.debug or os.environ.get('FLASK_ENV') == 'development':
        console_handler = logging.StreamHandler()
        console_handler.setLevel(logging.DEBUG)
        console_handler.setFormatter(formatter)
        logging.getLogger().addHandler(console_handler)
